package com.wanglemei.util;

import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailUtils {
    public static boolean isBlankString(String str) {
        if (null == str || "".equals(str)) {
            return true;
        }

        return false;
    }


   /**
    * 验证邮件地址
    * @param email
    * @return
    */
    public static Boolean validateEmailAddress(String email) {
        String filter = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
        Pattern mailPattern = Pattern.compile(filter);
        Matcher m = mailPattern.matcher(email);
        if (m.find()) {
            return true;
        }

        return false;
    }



    /**
     * 返回一个字符的长度。(汉字和汉字符号按长度为2计算，英文、英文符号和数字按长度为1计算)
     * @param c
     * @return
     */
    private static int getCharLen(String c) {
        if (c.length() != 1) {
            System.out.println("ERROR");
            return 0;
        }
        String reg = "^[0-9a-zA-Z`~!@#$%^&*()_+|\\-\\=\\\\,./<>?;':\"\\[\\]\\{\\}]+$";
        Pattern mailPattern = Pattern.compile(reg);
        Matcher m = mailPattern.matcher(c);
        if (m.find()) {
            return 1;
        }
        return 2;
    }


    /**
     * 截取字符串，len为截取之后的字符串长度（汉字和汉字符号按长度为2计算，英文、英文符号和数字按长度为1计算）
     * @param a
     * @param len
     * @return
     */
    public static String subString(String a, int len) {
        char[] arrs = new char[a.length()];
        a.getChars(0, a.length(), arrs, 0);
        int charLen = 0;
        int i = 0;
        for (; i < arrs.length; i++) {
            charLen += getCharLen(arrs[i] + "");
            if (charLen >= len) {
                break;
            }
        }
        return a.substring(0, i);
    }
    
    /**
     * 返回字符串的长度（汉字和汉字符号按长度为2计算，英文、英文符号和数字按长度为1计算）
     * @param string
     * @return
     */
    public static int getCharLenth(String string){
        char[] arrs = new char[string.length()];
        string.getChars(0, string.length(), arrs, 0);
        int charLen = 0;
        for (char c : arrs) {
            charLen += getCharLen(c + "");
        }
        return charLen;
    }
    

    /**
     * 截取邮件地址域名如123@qq.com ，qq.com
     * @param mail
     * @return
     */
    public static String getMspByMail(String mail)
    {
        try
        {
            String regex = "@(.*)"; 
            Pattern emailPattern = Pattern.compile(regex);
            Matcher matcher = emailPattern.matcher(mail);
            if(matcher.find())
            {
                String msp = matcher.group(1).toLowerCase();
                return msp;
            }
            return null;
        }
        catch(Exception e)
        {
            return null;
        }
        
    }

    public static String getMd5Str(String s)
	{
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
		try
		{
			byte[] strTemp = s.getBytes();

			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++)
			{
				byte b = md[i];

				str[k++] = hexDigits[b >> 4 & 0xf];
				str[k++] = hexDigits[b & 0xf];
			}
			return new String(str);
		}
		catch (Exception e)
		{
			return "";
		}
	}
    
    /**
     * @author bi_fengbo
     * @date 20121023
     * @param str
     * @return
     */
    public static String reverseStr(String str)
    {
    	if(str == null){return str;}
    	StringBuffer bf = new StringBuffer(str);
    	bf.reverse();
    	return bf.toString();
    }
    
    public static void main(String[] args) {
    	
        String a = "仅售37.9元！市场价127元的特级和田贵妃枣（500g）一袋！2袋包邮不足两袋另";
        
        System.out.println(EmailUtils.getMd5Str(a));
        
        String str =  "fengbo@lashou-inc.com796d3d99c18b123c78c74ef3618d9d80bfaa18ec34062a68";
        
        System.out.println(EmailUtils.reverseStr(EmailUtils.getMd5Str(str)));
        
        System.out.println(validateEmailAddress("1233qq.com"));
        
        System.out.println(subString("123@qq.com",3));
        
        System.out.println(getMspByMail("123@qq.com"));
        
    }


}
